package com.college.controller;

import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdminPortalController {
	
	
	@RequestMapping("adminportal")
	public ModelAndView adminPortalLogin(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		
		ModelAndView modelAndView = new ModelAndView("technicalDifficultiesPage");;
		HttpSession session=request.getSession(true);
		
		// reading properties file
		Properties prop = new Properties();
		InputStream input;
		try{
			input = this.getClass().getClassLoader().getResourceAsStream("config.properties");
		}catch(Exception e){
			modelAndView = new ModelAndView("technicalDifficultiesPage");
			return modelAndView;
		}
		
		prop.load(input);
	
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String userNameAdmin = prop.getProperty("adminUserName");
		String passwordAdmin = prop.getProperty("adminPassword");
				
		// checking user in session
		if("admin".equals(session.getAttribute("user"))){
			modelAndView = new ModelAndView("adminportal");
			return modelAndView;
		}else if(userName==null || password==null){
			modelAndView = new ModelAndView("technicalDifficultiesPage");
			return modelAndView;
		}
		
		// checking user credentials
		if(userName.equals(userNameAdmin)&&password.equals(passwordAdmin)){			
			session.setAttribute("user", "admin");
			modelAndView = new ModelAndView("adminportal");
			return modelAndView;
		}else if(!userName.equals(userNameAdmin)){
			modelAndView = new ModelAndView("adminLoginPage");
			modelAndView.addObject("errorMessage", "Invalid username !!!");
			return modelAndView;
		}else if(userName.equals(userNameAdmin)&&!password.equals(passwordAdmin)){
			modelAndView = new ModelAndView("adminLoginPage");
			modelAndView.addObject("errorMessage", "Invalid password !!!");
			return modelAndView;
		}else{
			modelAndView = new ModelAndView("technicalDifficultiesPage");
			return modelAndView;
		}
		
	}
	@RequestMapping("studentAdmissions")
	public ModelAndView adminStudentPortal(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		return new ModelAndView("adminPortalForAdmission");

	}
	@RequestMapping("studentRegistration")
	public ModelAndView studentRegistration(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		return new ModelAndView("studentRegistration");

	}
	@RequestMapping("studentDetailsUpdate")
	public ModelAndView studentDetailsUpdate(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		return new ModelAndView("studentDetailsUpdate");

	}
	@RequestMapping("studentDetailsRemove")
	public ModelAndView studentDetailsRemove(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		return new ModelAndView("studentDetailsRemove");

	}
	@RequestMapping("courseDetails")
	public ModelAndView courseDetails(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		return new ModelAndView("courseDetails");

	}
	@RequestMapping("courseDetailsModification")
	public ModelAndView courseDetailsModification(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		return new ModelAndView("courseDetailsModification");

	}
	@RequestMapping("adminlogin")
	public ModelAndView adminlogin(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		return new ModelAndView("adminLoginPage");

	}
	@RequestMapping("adminlogout")
	public ModelAndView adminlogout(HttpServletRequest request,
	HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession(true);
		session.removeAttribute("user");
		return new ModelAndView("adminLoginPage");

	}

}
